﻿(function (app) {
    app.controller('loginCtrl', function ($scope, userService, storage,$ionicHistory, $state, msg, $http) {
        $scope.vm = {
            mobile: '',
            password: ''
        };
        $scope.login = function () {
            $state.go('app.home');
            return;
            msg.loading('登录中...');
            userService.login($scope.vm.mobile, $scope.vm.password).then(function (result) {
                msg.hide();
                storage.setObject('user', result.data.data.user);
                storage.set('token', result.data.data.token);
                $http.defaults.headers.common["Authorization"] = 'Bearer ' + result.data.data.token;
                $state.go('app.borrow');
            }, function (erro) {
                console.log("login erro:"+JSON.stringify(erro));
                msg.hide();
                msg.error(erro.data.message);
            });
        }
    });
    app.controller('registerCtrl',  function ($scope, msg, util, userService, $timeout, $state, storage, $http, common) {
        $scope.isFirst = true;
        $scope.vm = {
            mobile: '',
            password: '',
            password1: '',
            verify_code: '',
            waitSeconds: "获取验证码",
            enableSend: true,
            type: 1,
            province:'',
            city:'',
            district:''
        };
      
        
        $scope.next = function () {
            //if (!util.isMobile($scope.vm.mobile)) {
            //    msg.text('请输入正确的手机号', true);
            //    return;
            //}
            if (!$scope.vm.verify_code) {
                msg.text('请输入短信验证码');
                return;
            }
            if ($scope.vm.password !== $scope.vm.password1) {
                msg.text('密码前后输入不一致');
                return;
            }
            msg.loading('请稍等...');
            userService.register({
                phone: $scope.vm.mobile, password: $scope.vm.password,
                verify_code: $scope.vm.verify_code, type: 2
            }).then(function (result) {
                storage.setObject('user', result.data.data.user);
                storage.set('token', result.data.data.token);
                $http.defaults.headers.common["Authorization"] = 'Bearer ' + result.data.data.token;
                msg.hide();
                $scope.isFirst = false; 
            }, function (error) {
                msg.hide();
                console.log(error);
                msg.error(error.data.message);
            });
        }
        $scope.save = function () {
            msg.loading('请稍等...');
            if ($scope.files[0]) {
                $scope.vm.images_0 = $scope.files.join(',');
            } 
            userService.profile($scope.vm).then(function (result) {
                msg.hide();
                if(localStorage['jpush']){
                    userService.jpush({ jpush: localStorage['jpush']}).then(function (result) {}, function (erro) {});
                }
                $state.go('mc.home');
            }, function (erro) {
                msg.hide();
                msg.error(erro.data.message);
            });
        } 
        $scope.getRegisterCode = function () {
            //if (!util.isMobile($scope.vm.mobile)) {
            //    msg.text('请输入正确的手机号', true);
            //    return;
            //}
            
            if ($scope.vm.waitSeconds!="获取验证码") {
               msg.text('请稍后再试', true);
               return;
            }
            $scope.vm.waitSeconds = "正在发送";
            userService.getRegisterCode($scope.vm.mobile).then(function (result) {
                wait(60);
            }, function (erro) {
                $scope.vm.waitSeconds = "获取验证码";
                wait(0);
                msg.error(erro.data.message);
            });
        };
        var wait = function (seconds) {
            if (seconds > 0) {
                $scope.vm.waitSeconds = "" + seconds + "秒";
            } else {
                $scope.vm.waitSeconds = "获取验证码";
            }
            $timeout(function () {
                if (seconds >= 1)
                    wait(seconds - 1);
            }, 1000);
        };

    });
    app.controller('forgetPasswordCtrl', ["$scope", "util", 'userService', '$timeout', '$state', 'msg', function ($scope, util, userService, $timeout, $state, msg) {
        $scope.vm = {
            mobile: '',
            code: '',
            password: '',
            password1: '',
            waitSeconds: "获取验证码"
        };

        $scope.getVerifyCode = function () {
            //if (!util.isMobile($scope.vm.mobile)) {
            //    msg.text('请输入正确的手机号', true);
            //    return;
            //}
            if ($scope.vm.waitSeconds!="获取验证码") {
               msg.text('请稍后再试', true);
               return;
            }
            $scope.vm.waitSeconds = "正在发送";
            userService.getVerifyCode($scope.vm.mobile).then(function (result) {
                wait(60);
            }, function (erro) {
                $scope.vm.waitSeconds = "获取验证码";
                wait(0);
                msg.error(erro.data.message);
            });
        }
        $scope.reset = function (vm) {
            if (vm.password != vm.password1) {
                msg.text('密码前后输入不一致');
                return;
            }
            msg.loading('请稍等...');
            var model = { phone: vm.mobile, verify_code: vm.code, password: vm.password, type:2 };
            userService.resetPassword(model).then(function () {
                msg.hide();
                msg.success('密码找回成功');
                $state.go('login');
            }, function (erro) {
                msg.hide();
                msg.error(erro.data.message);
            });
        }
        var wait = function (seconds) {
            if (seconds > 0) {
                $scope.vm.waitSeconds = "" + seconds + "秒";
            } else {
                $scope.vm.waitSeconds = "获取验证码";
            }
            $timeout(function () {
                if (seconds >= 1)
                    wait(seconds - 1);
            }, 1000);
        };
    }]);
})(angular.module('app.controllers'));